package haois.module.navigate.rest;

import haois.constant.Reply;
import haois.constant.ReturnState;
import haois.module.navigate.daomain.Navigate;
import haois.module.navigate.service.NavigateService;
import haois.security.HandleToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

import static haois.constant.StatusCode.SUBJECT_KEY_ID;

/**
 * Created by RuiHaoZ on 2018/4/20 10:53
 */
@RestController
@RequestMapping("/api/web")
public class NavigateController {
    @Autowired
    private NavigateService navigateService;

    private HandleToken handleToken = new HandleToken();

    @PostMapping(value = "/navigate", params = {"name", "mark"})
    public String addNavigation(Navigate navigate, HttpServletRequest request) {
        String mid = handleToken.getWebSubject(request, SUBJECT_KEY_ID);
        navigate.setCreateMan(mid);

        navigateService.addNavi(navigate);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @DeleteMapping("/navigate/{id}")
    public String deleteNavi(@PathVariable Integer id) {
        navigateService.deleteNavi(id);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @PutMapping(value = "/navigate/{id}", params = {"name"})
    public String updateNavi(@PathVariable Integer id, Navigate navigate) {
        navigate.setId(id);
        navigateService.updateNavi(navigate);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @GetMapping("/navigate/{id}")
    public String getNavi(@PathVariable Integer id) {
        Map<String, Object> navigation = navigateService.getNavi(id);
        return new Reply<Map<String, Object>>(ReturnState.CODE_00, ReturnState.MSG_00, navigation).toJson();
    }

    @Cacheable(value = "urlList_")
    @GetMapping("/url/list/{id}")
    public String getUrlByNaviId(@PathVariable Integer id) {
        List<Map<String, Object>> navis = navigateService.getUrlByNaviId(id);
        return new Reply<List<Map<String, Object>>>(ReturnState.CODE_00, ReturnState.MSG_00, navis).toJson();
    }
    //,unless="#result.data.name.contains('nocache')"
    //,key="#root.targetClass.getName() +'.'+ #root.methodName + #id"
    @Cacheable(value = "urlList")
    @GetMapping("/url/list/mark")
    public String getUrlByMark(String mark) {
        Integer id = navigateService.getId(mark);
        if (id == null) {
            return new Reply(ReturnState.CODE_02, ReturnState.MSG_02).toJson();
        }
        List<Map<String, Object>> navis = navigateService.getUrlByNaviId(id);
        return new Reply<List<Map<String, Object>>>(ReturnState.CODE_00, ReturnState.MSG_00, navis).toJson();
    }

    @GetMapping("/navigate/list")
    public String getNaviByMan(HttpServletRequest request) {
        String account = handleToken.getWebSubject(request);
        List<Map<String, Object>> navis = navigateService.getNaviByMan(account);
        return new Reply<List<Map<String, Object>>>(ReturnState.CODE_00, ReturnState.MSG_00, navis).toJson();
    }

    @PostMapping(value = "/url/{naviId}", params = {"name", "url"})
    public String addUrl(@PathVariable Integer naviId, String name, String url) {

        Map<String, Object> navi = navigateService.getNavi(naviId);
        if (navi == null) {
            return new Reply(ReturnState.CODE_02, ReturnState.MSG_02).toJson();
        }
        Navigate navigate = new Navigate();
        navigate.setId(naviId);
        navigate.setName(name);
        navigate.setUrl(url);
        navigateService.addUrl(navigate);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @DeleteMapping("/url/{id}")
    public String deleteUrl(@PathVariable Integer id) {
        navigateService.deleteUrl(id);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @PutMapping(value = "/url/{id}", params = {"name", "url"})
    public String updateUrl(@PathVariable Integer id,Navigate navigate) {
        navigate.setId(id);
        navigateService.updateUrl(navigate);
        return new Reply(ReturnState.CODE_00, ReturnState.MSG_00).toJson();
    }

    @GetMapping("/url/{id}")
    public String getUrl(@PathVariable Integer id) {
        Map<String, Object> url = navigateService.getUrl(id);
        return new Reply<Map<String, Object>>(ReturnState.CODE_00, ReturnState.MSG_00, url).toJson();
    }


}
